package fun.boomcat.luckyhe.mirai.plugin.luckymoney.command;

import fun.boomcat.luckyhe.mirai.plugin.luckymoney.service.MoneyService;
import fun.boomcat.luckyhe.mirai.plugin.luckymoney.service.impl.MoneyServiceImpl;
import fun.boomcat.luckyhe.mirai.plugin.luckymoney.util.ConfigUtil;
import fun.boomcat.luckyhe.mirai.plugin.luckymoney.util.OpMoneyCommandStr;
import kotlin.Unit;
import kotlin.coroutines.Continuation;
import net.mamoe.mirai.console.command.CommandManager;
import net.mamoe.mirai.console.command.CommandOwner;
import net.mamoe.mirai.console.command.CommandSender;
import net.mamoe.mirai.console.command.RawCommand;
import net.mamoe.mirai.console.permission.Permission;
import net.mamoe.mirai.message.data.MessageChain;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;

/**
 * @author Lucky_He
 * @date 2021年9月5日 14:47
 * @description OpMoneyCommand
 */
public class OpMoneyCommand extends RawCommand {

    public OpMoneyCommand(@NotNull CommandOwner owner, @NotNull String primaryName, @NotNull String[] secondaryNames, @NotNull String usage, @NotNull String description, @NotNull Permission parentPermission, boolean prefixOptional) {
        super(owner, primaryName, secondaryNames, usage, description, parentPermission, prefixOptional);
    }

    @Nullable
    @Override
    public Object onCommand(@NotNull CommandSender commandSender, @NotNull MessageChain messageChain, @NotNull Continuation<? super Unit> continuation) {
        int mcLen = messageChain.size();
        String primaryName = getPrimaryName();
        String[] secondaryNames = getSecondaryNames();

        if (mcLen == 0) {
            commandSender.sendMessage(OpMoneyCommandStr.getCommandHelp(primaryName, secondaryNames));
            return null;
        }

        String operation = messageChain.get(0).toString();
        switch (operation.toLowerCase()) {
            case "init":
            case "初始化":
                onInit(commandSender, primaryName);
                break;

            case "moneyadd":
            case "加钱":

                onMoneyAdd(commandSender, Arrays.copyOfRange(messageChain.toArray(), 1, mcLen), primaryName);
                break;
            case "moneydel":
            case "扣钱":
                onMoneyDel(commandSender, Arrays.copyOfRange(messageChain.toArray(), 1, mcLen), primaryName);
                break;

            default:

                commandSender.sendMessage(OpMoneyCommandStr.getCommandHelp(primaryName, secondaryNames));
                break;
        }

        return null;
    }

    private void onInit(CommandSender commandSender, String primaryName) {
        MoneyService service = new MoneyServiceImpl();
        try {
            service.createMoneyDB();
            commandSender.sendMessage("创建数据库完成！");
        } catch (Exception e) {
            commandSender.sendMessage("创建数据库失败：" + e.toString());
        }

        try {
            service.createMoneyTable();
            commandSender.sendMessage("创建金钱表完成！");
        } catch (Exception e) {
            commandSender.sendMessage("创建金钱表失败：" + e.toString());
        }
    }

    private void onMoneyAdd(CommandSender commandSender, Object[] args, String primaryName) {
        long qq;
        int money;

        String commandPrefix = CommandManager.INSTANCE.getCommandPrefix();

        try {
            qq = Long.parseLong(args[0].toString());
            money = Integer.parseInt(args[1].toString());
        } catch (Exception e) {
            commandSender.sendMessage(commandPrefix + primaryName + "moneyadd <QQ号> <钱数>    给指定用户加指定钱");
            return;
        }

        MoneyServiceImpl service = new MoneyServiceImpl();
        try {
            service.addUserMoneyByQq(qq, money);
        } catch (Exception e) {
            commandSender.sendMessage("发生错误，请稍后重试");
            return;
        }

        try {
            commandSender.sendMessage("已给QQ用户" + qq + "增加钱款 " + money + " " + ConfigUtil.getMoneyUnit() +
                    "\n当前有：" + service.getUserMoneyByQq(qq).getMoney() + " " + ConfigUtil.getMoneyUnit());
        } catch (Exception e) {
            commandSender.sendMessage("发生错误，请稍后重试（此错误发生在添加成功后）");
        }
    }

    private void onMoneyDel(CommandSender commandSender, Object[] args, String primaryName) {
        long qq;
        int money;

        String commandPrefix = CommandManager.INSTANCE.getCommandPrefix();

        try {
            qq = Long.parseLong(args[0].toString());
            money = Integer.parseInt(args[1].toString());
        } catch (Exception e) {
            commandSender.sendMessage(commandPrefix + primaryName + "moneydel <QQ号> <钱数>    给指定用户减指定钱");
            return;
        }

        MoneyServiceImpl service = new MoneyServiceImpl();
        try {
            service.takeUserMoneyByQq(qq, money);
        } catch (Exception e) {
            commandSender.sendMessage("发生错误，请稍后重试");
            return;
        }

        try {
            commandSender.sendMessage("已给QQ用户" + qq + "减少钱款 " + money + " " + ConfigUtil.getMoneyUnit() +
                    "\n当前有：" + service.getUserMoneyByQq(qq).getMoney() + " " + ConfigUtil.getMoneyUnit());
        } catch (Exception e) {
            commandSender.sendMessage("发生错误，请稍后重试（此错误发生在减少成功后）");
        }
    }
}
